//  引入路由


import VueRouter from "vue-router";

// 引入组件
//登录组件
import Login from '../components/Login'

// 系统页面
import Home from '../components/Home'

//  开始显示
import Wecome from '../components/Wecome'

// 用户列表
import Users from '../components/user/Users'

// 权限列表
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'

// 商品列表
import Cate from '../components/goods/Cate'

// 参数列表
import Params from '../components/goods/Params'

// 商品列表
import goods from '../components/goods/goods'
import Add from  '../components/goods/Add'

// 订单管理
import Order from '../components/order/Order'


// 图表
import Report from '../components/report/Report'

const route= new VueRouter(
    {
        routes:[
            {
                path:'/',
                redirect:'login',
            },
            {
                
                path:'/login',
                component:Login,
                meta:{title:'登陆页面'}
            },
            {
                path:'/home',
                component:Home,
                meta:{title:'后台页面'},
                redirect:'/wecome',
                children:[
                    {
                        path:'/wecome',
                        component:Wecome,
                        meta:{title:'电商后台管理系统'},
                    },
                    
                    {   
                        // 用户列表
                        path:'/users',
                        component:Users,
                        meta:{title:'用户列表'}
                    },
                    {
                        // 限权管理路由
                        path:'/rights',
                        component:Rights,
                        meta:{title:'权限列表'},
                    },
                    {
                        path:'/roles',
                        component:Roles,
                        meta:{title:'权限列表'},
                    },
                    {
                        path:'/categories',
                        component:Cate,
                        meta:{title:'商品列表'}
                    },
                    {
                        // 商品参数路由
                        path:'/params',
                        component:Params,
                        meta:{title:'商品参数'},
                    },
                    {
                        // 商品列表路由
                        path:'/goods',
                        component:goods,
                        meta:{title:'商品列表'},
                    },
                    {
                        // 商品列表路由
                        path:'/goods/add',
                        component:Add,
                        meta:{title:'添加商品'},
                    },
                    {
                        path:'/orders',
                        component:Order,
                        meta:{title:'订单管理'}
                    },
                    {
                        path:'/reports',
                        component:Report,
                        meta:{title:'图表列表'}
                    }
                ]
            }
        ]
    }
)

//  路由守卫 当登录成功时才有权限查看 否则回到登录页面

route.beforeEach((to,from,next)=>{
    if(to.meta.isAut){
        // 获取token
        const logintoken=window.sessionStorage.getItem('token')

        // 判断是否存在token 存在则放行 否则强制到login
        if(!logintoken) return next('/login')
        next()

    }else{
        next()
    }
    
})


route.afterEach((to,from,next)=>{
        if(to.meta.title)
        {
            document.title=to.meta.title;
           
        }
        else
        {
            document.title='Vue'
        }
})


export default route